package com.hpin.mlxteacher.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.ChooseAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.People;
import com.hpin.mlxteacher.widget.GridViewForScrollView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 选择收件人页面
 * Created by Aaron on 2015/7/21.
 */
public class ReceiverActivity extends BaseActivity {


    private ChooseAdapter chooseAdapter;
    private TextView tv_school;
    private TextView tv_child;
    private List<People> peoples;
    /**收件人code*/
    private static final int RECEIVER_CODE = 1008;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_receiver);

        initWidget();

    }

    private void initWidget() {

        TextView tv_left = (TextView) findViewById(R.id.tv_left);

        tv_school = (TextView) findViewById(R.id.tv_school);
        tv_child = (TextView) findViewById(R.id.tv_child);

        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_left.setText(getResources().getString(R.string.select_all));
        tv_right.setText(getResources().getString(R.string.done));

        onSchoolSelect();

        GridViewForScrollView gv_receiver = (GridViewForScrollView) findViewById(R.id.gv_peoplel);

        peoples = new ArrayList<People>();
        People p1 = new People();
        p1.name = "张大大";
        People p2 = new People();
        p2.name = "张小小";
        People p3 = new People();
        p3.name = "张大小";
        People p4 = new People();
        p4.name = "张小大";
        People p5 = new People();
        p5.name = "张轩轩";
        People p6 = new People();
        p6.name = "李大大"; People p7 = new People();
        p7.name = "张大大";
        People p8 = new People();
        p8.name = "张小小";
        People p9 = new People();
        p9.name = "张大小";
        People p10 = new People();
        p10.name = "张小大";
        People p15 = new People();
        p15.name = "张轩轩";
        People p16 = new People();
        p16.name = "李大大"; People p11 = new People();
        p11.name = "张大大";
        People p12 = new People();
        p12.name = "张小小";
        People p13 = new People();
        p13.name = "张大小";
        People p14 = new People();
        p14.name = "张小大";
        People p17 = new People();
        p17.name = "张轩轩";
        People p18 = new People();
        p18.name = "李大大"; People p19 = new People();
        p19.name = "张大大";
        People p20 = new People();
        p20.name = "张小小";
        People p23 = new People();
        p23.name = "张大小";
        People p24 = new People();
        p24.name = "张小大";
        People p25 = new People();
        p25.name = "张轩轩";
        People p26 = new People();
        p26.name = "李大大";
        peoples.add(p1);
        peoples.add(p2);
        peoples.add(p3);
        peoples.add(p4);
        peoples.add(p5);
        peoples.add(p6); peoples.add(p11);
        peoples.add(p12);
        peoples.add(p13);
        peoples.add(p14);
        peoples.add(p15);
        peoples.add(p16); peoples.add(p7);
        peoples.add(p8);
        peoples.add(p9);
        peoples.add(p10);
        peoples.add(p17);
        peoples.add(p18); peoples.add(p19);
        peoples.add(p20);
        peoples.add(p23);
        peoples.add(p24);
        peoples.add(p25);
        peoples.add(p26);
        chooseAdapter = new ChooseAdapter(this, peoples);
        gv_receiver.setAdapter(chooseAdapter);

        gv_receiver.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //反选 收件人
//                ((People) chooseAdapter.getItem(position)).isSelect =  (((People) chooseAdapter.getItem(position)).isSelect) == 0 ? 1 : 0;
                ((People) chooseAdapter.getItem(position)).isSelect =  !((People) chooseAdapter.getItem(position)).isSelect;

                chooseAdapter.notifyDataSetChanged();
            }
        });

        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        tv_school.setOnClickListener(this);
        tv_child.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.tv_left:
                //全选

                for (People p : peoples) {

                    p.isSelect = true;
                    chooseAdapter.notifyDataSetChanged();
                }


                break;
            case R.id.tv_right:
                //完成


                List<People> chooseedPeoples=new ArrayList<People>();

                for (People p : peoples) {

                    if(p.isSelect) {
                        chooseedPeoples.add(p);
                    }
                }
                Intent data = new Intent();
                data.putExtra("chooseedPeoples" , (Serializable)chooseedPeoples);
                setResult(RECEIVER_CODE, data);
                finish();


                break;
            case R.id.tv_school:
                //学校


                onSchoolSelect();


                break;
            case R.id.tv_child:
                //孩子
                onChildSelect();


                break;


        }
    }


    /**
     * 选中了学校 改变字体和背景颜色
     */
    private void onSchoolSelect() {
        tv_school.setTextColor(getResources().getColor(R.color.red));
        tv_school.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_bg_white));
        tv_child.setTextColor(getResources().getColor(R.color.white));
        tv_child.setBackgroundDrawable(null);
    }

    /**
     * 选中了孩子 改变字体和背景颜色
     */
    private void onChildSelect() {

        tv_child.setTextColor(getResources().getColor(R.color.red));
        tv_child.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_bg_white));
        tv_school.setTextColor(getResources().getColor(R.color.white));
        tv_school.setBackgroundDrawable(null);
    }
}
